package com.yihu.wlyy.service.app.message;

import java.util.HashMap;
import java.util.Map;

import javax.transaction.Transactional;

import com.yihu.wlyy.entity.Message;
import com.yihu.wlyy.repository.MessageDao;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import com.yihu.wlyy.entity.consult.ConsultTeam;
import com.yihu.wlyy.repository.ConsultTeamDao;
import com.yihu.wlyy.service.BaseService;

/**
 * 消息业务处理类
 * @author George
 *
 */
@Component
@Transactional(rollbackOn = Exception.class)
public class MessageService extends BaseService {

	@Autowired
	private ConsultTeamDao consultTeamDao;
	@Autowired
	private MessageDao messageDao;

	/**
	 * 汇总查询医生的消息总数
	 * @param doctor
	 * @return
	 */
	public JSONObject findDoctorAllMessageAmount(String doctor) {
		// 三师咨询未读消息总数
		int consult = 0;
		try {
			consult = consultTeamDao.amountAllDoctorUnread(doctor);
		} catch (Exception e) {
		}
		// 签约未读消息总数
		int sign = messageDao.amountUnreadByReceiver(doctor);
		// 体征指标未读消息总数
		int healthIndex = messageDao.amountUnreadHealthByReceiver(doctor);
		JSONObject json = new JSONObject();
		json.put("consultTeam", consult);
		json.put("sign", sign);
		json.put("healthIndex", healthIndex);
		return json;
	}

	/**
	 * 查询健康咨询列表
	 * @param doctor 医生标识
	 * @param id
	 * @param pagesize 分页大小
	 * @return
	 */
	public Page<ConsultTeam> findConsultListByDoctor(String doctor, long id, int pagesize) {
		if (pagesize <= 0) {
			pagesize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);
		if (id > 0) {
			return consultTeamDao.findListByUnreadDoctor(doctor, id, pageRequest);
		} else {
			return consultTeamDao.findListByUnreadDoctor(doctor, pageRequest);
		}
	}

	/**
	 * 查询体征消息列表
	 * @param doctor 医生标识
	 * @param id
	 * @param pagesize 分页大小
	 * @return
	 */
	public Page<Message> findHealthListByDoctor(String doctor, long id, int pagesize, String isRead) {
		if (pagesize <= 0) {
			pagesize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);
		// 设置查询条件
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		filters.put("receiver", new SearchFilter("receiver", Operator.EQ, doctor));
		filters.put("type", new SearchFilter("type", Operator.EQ, "2"));
		if (id > 0) {
			filters.put("id", new SearchFilter("id", Operator.LT, id));
		}
		if (StringUtils.isNoneEmpty(isRead)) {
			filters.put("read", new SearchFilter("read", Operator.EQ, Integer.parseInt(isRead)));
		}
		Specification<Message> spec = DynamicSpecifications.bySearchFilter(filters.values(), Message.class);
		return messageDao.findAll(spec, pageRequest);
	}

	/**
	 * 更新体征消息为已读
	 * @param msgid
	 */
	public int readHealth(long msgid) {
		return messageDao.read(msgid);
	}

	/**
	 * 根据参数查询指定的消息
	 * @return
	 */
	public Message findMessage(String sender, String receiver, String signStatus) {

		return messageDao.findByParams(sender, receiver, signStatus);
	}

}
